package com.ds.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: SpringBoot
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-04-18
 **/
//添加拦截配置
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (StringUtils.isEmpty(loginUser)){
            request.setAttribute("errorMsg","<span style='color:red'>请先登录</span>");
            //如果Session为空,转发到登陆页面
            request.getRequestDispatcher("/").forward(request,response);
            System.out.println("认证不通过");
            return false;
        }else {
            System.out.println("认证通过");
            return true;
        }

    }
}
